﻿public partial class phonecall : System.Web.UI.Page
{
	/// crmService
	private Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy crmService = CRM2011.Service.Connection.GetService();

	/// OnLoad
	protected override void OnLoad(System.EventArgs e)
	{
		// call base
		base.OnLoad(e);

		// get contact
		Microsoft.Xrm.Sdk.Entity contactObj = GetContactByPhoneNumber(Request["phoneNr"]);
		Microsoft.Xrm.Sdk.Entity userObj = GetUserByDomainname("ESPRESSOFT-TECH\\alexandras");
		CreatePhoneCallAndOpen(contactObj, userObj);
	}

	/// GetContactByPhoneNumber
	private Microsoft.Xrm.Sdk.Entity GetContactByPhoneNumber(System.String phoneNr)
	{
		// Create the conditionExpressionLastname
		Microsoft.Xrm.Sdk.Query.ConditionExpression conditionExpressionPhoneNr =
			new Microsoft.Xrm.Sdk.Query.ConditionExpression(
				"address1_telephone1",
				Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,
				phoneNr);

		// Create the FilterExpression.
		Microsoft.Xrm.Sdk.Query.FilterExpression criteriaExpression = new Microsoft.Xrm.Sdk.Query.FilterExpression();

		// Set the properties of the criteriaExpression.
		criteriaExpression.FilterOperator = Microsoft.Xrm.Sdk.Query.LogicalOperator.And;
		criteriaExpression.AddCondition(conditionExpressionPhoneNr);

		// Create the QueryExpression object.
		Microsoft.Xrm.Sdk.Query.QueryExpression query = new Microsoft.Xrm.Sdk.Query.QueryExpression();

		// Set the properties of the QueryExpression object.
		query.EntityName = "contact";
		query.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet(true);
		query.Criteria = criteriaExpression;

		// Retrieve the accounts.
		Microsoft.Xrm.Sdk.DataCollection<Microsoft.Xrm.Sdk.Entity> entityCollection = crmService.RetrieveMultiple(query).Entities;
		if (entityCollection.Count > 0)
		{
			return (Microsoft.Xrm.Sdk.Entity)entityCollection[0];
		}

		// none
		return null;
	}

	/// GetContactByPhoneNumber
	private Microsoft.Xrm.Sdk.Entity GetUserByDomainname(System.String domainname)
	{
		// Create the conditionExpressionLastname
		Microsoft.Xrm.Sdk.Query.ConditionExpression conditionExpressionDN =
			new Microsoft.Xrm.Sdk.Query.ConditionExpression(
				"domainname",
				Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,
				domainname);

		// Create the FilterExpression.
		Microsoft.Xrm.Sdk.Query.FilterExpression criteriaExpression = new Microsoft.Xrm.Sdk.Query.FilterExpression();

		// Set the properties of the criteriaExpression.
		criteriaExpression.FilterOperator = Microsoft.Xrm.Sdk.Query.LogicalOperator.And;
		criteriaExpression.AddCondition(conditionExpressionDN);

		// Create the QueryExpression object.
		Microsoft.Xrm.Sdk.Query.QueryExpression query = new Microsoft.Xrm.Sdk.Query.QueryExpression();

		// Set the properties of the QueryExpression object.
		query.EntityName = "systemuser";
		query.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet(true);
		query.Criteria = criteriaExpression;

		// Retrieve the accounts.
		Microsoft.Xrm.Sdk.DataCollection<Microsoft.Xrm.Sdk.Entity> entityCollection = crmService.RetrieveMultiple(query).Entities;
		if (entityCollection.Count > 0)
		{
			return (Microsoft.Xrm.Sdk.Entity)entityCollection[0];
		}

		// none
		return null;
	}

	/// GetContactByPhoneNumber
	private void CreatePhoneCallAndOpen(
		Microsoft.Xrm.Sdk.Entity contactObj,
		Microsoft.Xrm.Sdk.Entity userObj)
	{
		// to
		Microsoft.Xrm.Sdk.Entity toObj = new Microsoft.Xrm.Sdk.Entity("activityparty");
		toObj.Attributes.Add("partyid", new Microsoft.Xrm.Sdk.EntityReference("systemuser", userObj.Id));

		// from
		Microsoft.Xrm.Sdk.Entity fromObj = null;
		if (null != contactObj)
		{
			fromObj = new Microsoft.Xrm.Sdk.Entity("activityparty");
			fromObj.Attributes.Add("partyid", new Microsoft.Xrm.Sdk.EntityReference("contact", contactObj.Id));
		}

		Microsoft.Xrm.Sdk.Entity phoneCallNew = new Microsoft.Xrm.Sdk.Entity("phonecall");
		phoneCallNew.Attributes.Add("directioncode", false);
		phoneCallNew.Attributes.Add("to", new[] { toObj });
		phoneCallNew.Attributes.Add("phonenumber", Request["phoneNr"]);
		if (null != contactObj)
		{
			phoneCallNew.Attributes.Add("from", new[] { fromObj });
			phoneCallNew.Attributes.Add("new_clientid", new Microsoft.Xrm.Sdk.EntityReference("contact", contactObj.Id));
		}
		phoneCallNew.Attributes.Add("subject", "ContactCenter Lync call");

		// create
		System.Guid phoneCallGuid = crmService.Create(phoneCallNew);

		// url
		System.String url =
			System.String.Format(
				"http://ess-wdev-01:5555/CRMDEV/main.aspx?etc=4210&extraqs=%3f_gridType%3d4210%26etc%3d4210%26id%3d%257b{0}%257d%26pagemode%3diframe%26preloadcache%3d1359016550421%26rskey%3d339509484&pagetype=entityrecord",
				phoneCallGuid);

		// redirect
		Response.Redirect(url);
	}

}